package com.tangyee.person_risk_assessment.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.tangyee.person_risk_assessment.bean.UserAnswerStateBean;

import java.util.List;

public class AssessResultDAO {

    private AssessResultDataBaseHelper assessResultDataBaseHelper;
    private SQLiteDatabase db;

    public AssessResultDAO(Context context){
        assessResultDataBaseHelper = new AssessResultDataBaseHelper(context);
    }

    public void saveUserQuestionState(String tableName, List<UserAnswerStateBean> userAnswerStateBeanList){
        db = assessResultDataBaseHelper.getWritableDatabase();

        // 因为这个数据表的数据较少，而且保存评估问题的问题信息几乎要每一题都要去进行update 动作
        // 所以我选择先将所有的数据删除，再重新添加数据
        db.execSQL("DELETE FROM " + tableName);

        // 添加
        ContentValues values = new ContentValues();
        for (UserAnswerStateBean userAnswerStateBean : userAnswerStateBeanList){
            System.out.println("题号"+userAnswerStateBean.getQuestionNum() +"选项" +userAnswerStateBean.getQuestionOptionNum() + "分数"+userAnswerStateBean.getQuestionScore() +"问题点" +userAnswerStateBean.getQuestionPoint());
            values.put("QuestionNumber", userAnswerStateBean.getQuestionNum());
            values.put("UserQuestionOptionNumber", userAnswerStateBean.getQuestionOptionNum());
            values.put("QuestionScore", userAnswerStateBean.getQuestionScore());
            values.put("QuestionPoint", userAnswerStateBean.getQuestionPoint());
            db.insert(tableName, null, values);
        }

        db.close();

    }

    public int[] getQuestionOptionNumber(String tableName){
        int[] questionOptionNum;

        db = assessResultDataBaseHelper.getReadableDatabase();
        Cursor cursor = db.query(tableName,new String[]{"UserQuestionOptionNumber"},null, null,null,null,null);
        questionOptionNum = new int[cursor.getCount()];

        int i = 0;
        while (cursor.moveToNext()){
            questionOptionNum[i++] = cursor.getInt(0);
        }

        return questionOptionNum;
    }

    public int[] getQuestionScore(String tableName){
        int[] questionScore;

        db = assessResultDataBaseHelper.getReadableDatabase();
        Cursor cursor = db.query(tableName, new String[]{"QuestionScore"},null, null,null,null,null);
        questionScore = new int[cursor.getCount()];

        int i = 0;
        while (cursor.moveToNext()){
            questionScore[i++] = cursor.getInt(0);
        }

        db.close();

        return questionScore;
    }

    public String[] getAssessRiskFactor(String tableName){
        String[] assessRiskFactor;

        db = assessResultDataBaseHelper.getReadableDatabase();
        Cursor cursor = db.query(tableName, new String[]{"QuestionPoint"},null, null,null,null,null);
        assessRiskFactor = new String[cursor.getCount()];

        int i = 0;
        while (cursor.moveToNext()){
            assessRiskFactor[i++] = cursor.getString(0);
        }

        db.close();

        return assessRiskFactor;
    }

    public int getRecordNumber(String tableName){
        db = assessResultDataBaseHelper.getReadableDatabase();
        Cursor cursor = db.query(tableName,null,null,null,null,null,null);
        int recordNum = cursor.getCount();

        db.close();

        return recordNum;
    }

    public void deleteAllRecords(){

    }

}
